/**
 * @projectName study-note
 * @package middle
 * @className middle.FindNumberIn2DArray
 */
package middle;

/**
 * FindNumberIn2DArray
 * @description 二维 二分查找
 * @author answer_wx
 * @date 2022/7/8 21:43
 * @version 1.0
 */
public class FindNumberIn2DArray {
    static boolean findNumberIn2DArray(int[][] matrix, int target) {
        int row = matrix.length;
        if (row == 0) {
            return false;
        }
        int col = matrix[0].length;
        if (col == 0) {
            return false;
        }
        for (int i = 0; i < row; i++) {
            int s = 0, e = col - 1;
            if (target < matrix[i][s]) {
                break;
            }
            if (target > matrix[i][e]) {
                continue;
            }
            while (s <= e) {
                int mid = s + ((e - s) >> 1);
                if (matrix[i][mid] < target) {
                    s = mid + 1;
                } else if (matrix[i][mid] > target) {
                    e = mid - 1;
                } else {
                    return true;
                }
            }
        }
        return false;
    }


    public static void main(String[] args) {
        int[][] matrix = new int[][]{
        };
        System.out.println(findNumberIn2DArray(matrix, 20));
    }
}